package com.fire.user.controller;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.fire.core.service.ITMitErrorCodeService;
import com.fire.core.util.BlogUtil;
import com.fire.user.constants.ErrorCodeConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fire.core.base.controller.BaseController;
import com.fire.core.util.AjaxResult;
import com.fire.core.util.ToolUtil;
import com.fire.user.service.IClientService;

/**
 * 客户控制器
 * 
 * @author raoyuxing
 * @Date 2018-04-24 
 *
 */
@RestController
public class ClientController extends BaseController {
	
	private  static final Logger LOGGER=LoggerFactory.getLogger(ClientController.class);
	
	@Autowired
	private  IClientService clientService;
	@Autowired
	private ITMitErrorCodeService itMitErrorCodeService;
	
	/**
     * 注册功能获取验证码
     * @param json
     * @return AjaxResult
     */
    @RequestMapping(value="/addclient",method=RequestMethod.POST)
    @ResponseBody
    public AjaxResult addClientInfo(@RequestBody String json){
    	LOGGER.info("ClientController.addClientInfo获取到的json消息" + json);
		BlogUtil.inlog(json,"","mit/user/addclient","addClientInfo","req");
    	Map map = json2Map(json);
    	AjaxResult ajaxResult = new AjaxResult();

    	String seq = "seq";
    	String sourcetype = "sourcetype";
    	String relationsship = "relationsship";
    	Map application = (Map) map.get("application");
		Map insuredclient = (Map) map.get("insuredclient");
    	if (ToolUtil.isEmpty(map.get(seq))) {
    		LOGGER.error("ClientController.addClientInfo().debug  错误为：seq为空");
    		ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.USER_PARAMERROE,"信息不完整","信息不完整"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/addclient","addClientInfo","resp");
    		return ajaxResult;
		} else if (ToolUtil.isEmpty(map.get(sourcetype))){
			LOGGER.error("ClientController.addClientInfo().debug  错误为：sourcetype为空");
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.USER_PARAMERROE,"信息不完整","信息不完整"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/addclient","addClientInfo","resp");
			return ajaxResult;
		} else if (ToolUtil.isEmpty(map.get(relationsship))){
			LOGGER.error("ClientController.addClientInfo().debug  错误为：relationsship为空");
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.USER_PARAMERROE,"信息不完整","信息不完整"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/addclient","addClientInfo","resp");
			return ajaxResult;
		} else if (ToolUtil.isEmpty(application.get("applicationname"))){
			LOGGER.error("ClientController.addClientInfo().debug  错误为：applicationname为空");
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.USER_PARAMERROE,"信息不完整","信息不完整"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/addclient","addClientInfo","resp");
			return ajaxResult;
		} else if (ToolUtil.isEmpty(insuredclient.get("insuredname"))){
			LOGGER.error("ClientController.addClientInfo().debug  错误为：insuredname为空");
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.USER_PARAMERROE,"信息不完整","信息不完整"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/addclient","addClientInfo","resp");
			return ajaxResult;
		}else if (ToolUtil.isEmpty(application.get("idno"))){
			LOGGER.error("ClientController.addClientInfo().debug  错误为：application idno为空");
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.USER_PARAMERROE,"信息不完整","信息不完整"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/addclient","addClientInfo","resp");
			return ajaxResult;
		} else if (ToolUtil.isEmpty(insuredclient.get("idno"))){
			LOGGER.error("ClientController.addClientInfo().debug  错误为：insured idno为空");
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.USER_PARAMERROE,"信息不完整","信息不完整"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/addclient","addClientInfo","resp");
			return ajaxResult;
		}
    	Map context = null;
		try {
			context = clientService.addClientInfo(map);
		} catch (Exception e) {
			LOGGER.error("ClientController.addClientInfo().error  错误为：",e);
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.SYS_DATAERROE,"系统错误","系统错误"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/addclient","addClientInfo","resp");
			return ajaxResult;
		}
		ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.SUCCESS,"操作成功",context,"操作成功"));
		BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/user/addclient","addClientInfo","resp");
		return ajaxResult;
    }
}
